/***********************************************************************
	filename: 	CEGUIAnimateset_xmlHandler.cpp
	created:	21/2/2004
	author:		Paul D Turner

	purpose:	Implements the Animateset class
*************************************************************************/
/***************************************************************************
 *   Copyright (C) 2004 - 2006 Paul D Turner & The CEGUI Development Team
 *
 *   Permission is hereby granted, free of charge, to any person obtaining
 *   a copy of this software and associated documentation files (the
 *   "Software"), to deal in the Software without restriction, including
 *   without limitation the rights to use, copy, modify, merge, publish,
 *   distribute, sublicense, and/or sell copies of the Software, and to
 *   permit persons to whom the Software is furnished to do so, subject to
 *   the following conditions:
 *
 *   The above copyright notice and this permission notice shall be
 *   included in all copies or substantial portions of the Software.
 *
 *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 *   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 *   IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
 *   OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 *   ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 *   OTHER DEALINGS IN THE SOFTWARE.
 ***************************************************************************/
#include "CEGUIAnimateset_xmlHandler.h"

#include "CEGUIExceptions.h"
#include "CEGUISystem.h"
#include "CEGUILogger.h"
#include "CEGUIXMLAttributes.h"

// Start of CEGUI namespace section
namespace CEGUI
{

/*************************************************************************
	Definition of constant data for Animateset (and sub-classes)
*************************************************************************/
// Declared in Animateset::xmlHandler
const String Animateset_xmlHandler::AnimatesetElement( "Animateset" );
const String Animateset_xmlHandler::AnimateElement( "Animate" );
const String Animateset_xmlHandler::ImageElement( "Image" );

const String Animateset_xmlHandler::AnimatesetNameAttribute( "Name" );

const String Animateset_xmlHandler::AnimateNameAttribute( "Name" );
const String Animateset_xmlHandler::AnimateLoopAttribute( "Loop" );
const String Animateset_xmlHandler::AnimateDelayAttribute( "Delay" );

const String Animateset_xmlHandler::ImageNameAttribute( "Name" );

/*************************************************************************
	SAX2 Handler methods
*************************************************************************/
void Animateset_xmlHandler::elementStart(const String& element, const XMLAttributes& attributes)
{
    // handle an Animate element (extract all element attributes and use data to define an Animate for the Animateset)
	// handle root Animateset element
	if (element == ImageElement)
	{
		elementImageStart(attributes);
	}
	else if (element == AnimateElement)
    {
        elementAnimateStart(attributes);
    }
    // handle root Animateset element
    else if (element == AnimatesetElement)
    {
        elementAnimatesetStart(attributes);
    }
    // anything else is an error which *should* have already been caught by XML validation
    else
    {
        Logger::getSingleton().logEvent("Animateset::xmlHandler::startElement - Unexpected data was found while parsing the Animateset file: '" + element + "' is unknown.", Errors);
    }
}

void Animateset_xmlHandler::elementEnd(const String& element)
{
    if (element == AnimatesetElement)
    {
        elementAnimatesetEnd();
    }
}

/*************************************************************************
    Method that handles the opening Animateset XML element.
*************************************************************************/
void Animateset_xmlHandler::elementAnimatesetStart(const XMLAttributes& attributes)
{
    d_animateset->d_name = attributes.getValueAsString(AnimatesetNameAttribute);
        
    Logger::getSingleton().logEvent("Started creation of Animateset from XML specification:");
    Logger::getSingleton().logEvent("---- CEGUI Animateset name: " + d_animateset->d_name);    
}

/*************************************************************************
    Method that handles the Animate XML element.
*************************************************************************/
void Animateset_xmlHandler::elementAnimateStart(const XMLAttributes& attributes)
{
    String  name(attributes.getValueAsString(AnimateNameAttribute));
	bool bLoop = attributes.getValueAsBool(AnimateLoopAttribute);
	float fDelay = attributes.getValueAsFloat(AnimateDelayAttribute);

	m_pAnimate = new Animate(d_animateset,name,bLoop,fDelay);

	d_animateset->addAnimate(m_pAnimate);
}

/*************************************************************************
	Method that handles the Image XML element.
*************************************************************************/
void Animateset_xmlHandler::elementImageStart(const XMLAttributes& attributes)
{
	String  name(attributes.getValueAsString(ImageNameAttribute));

	m_pAnimate->addImage(name);
}

/*************************************************************************
	Method that handles the closing Animateset XML element.
*************************************************************************/
void Animateset_xmlHandler::elementAnimatesetEnd()
{
	Logger::getSingleton().logEvent("Finished creation of Animateset '" + d_animateset->d_name + "' via XML file.", Informative);
}

} // End of  CEGUI namespace section
